#!/bin/bash

source /home/shell/util.sh

if [ "$(ostype)" == "Ubuntu" ]; then
    apt install -y lvm2
else
    yum install -y lvm2
fi

fdisk -l |grep "Disk"|grep -v "Disklabel"|grep -v "identifier"|grep -v "model"

read -p "请输入待分区设备：" dev_path
if [[ -z "${dev_path}" ]]; then
    printFail "输入错误！";
    exit;
fi
read -p "请输入挂载文件夹[/ext-disk]：" mount_path
mount_path=${mount_path:-"/ext-disk"};

if [ ! -d "${mount_path}" ]; then
    mkdir -p ${mount_path}
fi


fdisk ${dev_path}<<EOF
n
p



t
8e
p
w
EOF

# fdisk -l|grep 8e
printInfo "挂载：${dev_path}1"
printInfo "创建PV..."
pvcreate ${dev_path}1
printInfo "创建VG(ext-vg)..."
vgcreate ext-vg ${dev_path}1
printInfo "创建LV(ext-lv)..."
lvcreate -l 100%free -n ext-lv ext-vg
printInfo "格式化(ext4)..."
mkfs.ext4 /dev/ext-vg/ext-lv

disk_uuid=`blkid /dev/mapper/ext--vg-ext--lv|perl -pe 's#.*UUID="(.*?)".*#\1#g'`
disk_format=`blkid /dev/mapper/ext--vg-ext--lv|perl -pe 's#.*TYPE="(.*?)".*#\1#g'`
tee /etc/fstab -a <<EOF
UUID=${disk_uuid} ${mount_path}               ${disk_format}    defaults        0 0
EOF
printInfo "挂载中..."
mount -a
printInfo "=========================磁盘信息========================="
df -h
printInfo "=========================磁盘信息========================="